/*
 * delete-item.c
 *
 *  Created on: 22-dic-2008
 *      Author: igor
 */

#include "view.h"


/* cancella una nota dall'elenco
 *
 * delete a note from list
 */
static void
my_bt_delete_item(void *data, Evas_Object *obj, void *event_info)
{
  printf("DEBUG: <my_bt_delete_item>;\n");

  struct todoitem *p = (struct todoitem *) leggi_items_da_db(p);

  struct todoitem *todolist = data;

  del_item_from_db(data);
  printf("DEBUG: </my_bt_delete_item>;\n");
  return;

}

/* mostra la finestra per la cancellazione di una nota esistente
 *
 * shows the window for the cancellation of an existing item,
 * it shows a list of button with the items in storage as title
 * clicking on the button it deletes the note.
 */
void
my_entry_bt_delete(void *data, Evas_Object *obj, void *event_info)
{
   printf ("DEBUG: <my_entry_bt_delete>\n");
   Evas_Object *win, *bg, *bx0, *lb, *bx, *bt, *sc, *fr, *ly, *sc_tit;
   char buf[PATH_MAX];
   Evas_Object *mother = data;

   win = elm_win_add(NULL, PACKAGE " item delete", ELM_WIN_BASIC);
   /* set the title of the window - this is in the titlebar */
   elm_win_title_set(win, PACKAGE " - item delete");
   elm_win_autodel_set(win, 1);

   bg = elm_bg_add(win);
   /* set weight to 1.0 x 1.0 == expand in both x and y direction */
   evas_object_size_hint_weight_set(bg, 1.0, 1.0);
   /* tell the window that this object is to be resized along with the window.
    * also as a result this object will be one of several objects that
    * controls the minimum/maximum size of the window */
   elm_win_resize_object_add(win, bg);
   /* and show the background */
   evas_object_show(bg);

   ly = elm_layout_add(win);
   snprintf(buf, sizeof(buf), "%s/%s.edj", PIXMAPS_DIR, PACKAGE);
   printf ("DEBUG layout: %s;\n", buf);
   elm_layout_file_set(ly, buf, "delete");
   elm_win_resize_object_add(win, ly);
   evas_object_show(ly);

   /* add a box layout widget to the window */
   bx0 = elm_box_add(win);
   window_swallow(ly, "title_bx", bx0);
   /* allow base box (bx0) to expand in x and y */
   evas_object_size_hint_weight_set(bx0, 1.0, 1.0);
   /* tell the window that the box affects window size and also will be
    * resized when the window is */
   elm_win_resize_object_add(win, bx0);
   evas_object_show(bx0);

   fr = elm_frame_add(win);
   window_swallow(ly, "title_frame", fr);
   elm_frame_label_set(fr, "delete item");
   elm_box_pack_end(bx0, fr);
   evas_object_show(fr);

   lb = elm_label_add(win);
   elm_label_label_set(lb,
      "slect a note from the list below by clicking<br>"
	  "the button to delete the todo-item.");
   elm_frame_content_set(fr, lb);
   evas_object_show(lb);

   /* add a scroller object - anything inside is scrollable */
   sc_tit = elm_scroller_add(win);
   window_swallow(ly, "title_sc", sc_tit);
   evas_object_size_hint_weight_set(sc_tit, 1.0, 1.0);
   elm_box_pack_end(bx0, sc_tit);
   evas_object_show(sc_tit);

  /* add a scroller object - anything inside is scrollable */
  sc = elm_scroller_add(win);
  window_swallow(ly, "contenitore_sc", sc);
  /* add scroller to main box */
  evas_object_size_hint_weight_set(sc, 1.0, 1.0);
  evas_object_show(sc);

  /* add a box layout widget to the window */
  bx = elm_box_add(win);
  /* set weight to 1.0 x 1.0 == expand in x and y) */
  evas_object_size_hint_weight_set(bx, 1.0, 0.0);

  /* set the box ad the content in the scrolled view*/
  elm_scroller_content_set(sc, bx);
  /* show the box, scroller and main box */
  evas_object_show(bx);

  struct todoitem *todolist = NULL;
  todolist = (struct todoitem *) leggi_items_da_db(todolist);

   if (todolist == NULL)
   {
      printf("DEBUG: lista vuota;\n");
   } else {

      while (todolist != NULL)
         {
//            printf("%s \t%s\n%s\n", todolist->inf.title, todolist->inf.infos, todolist->inf.text);

            bt = elm_button_add(win);
            elm_button_label_set(bt, todolist->inf.title);
            evas_object_smart_callback_add(bt, "clicked", my_win_close, win);
            evas_object_smart_callback_add(bt, "clicked", my_entry_bt_delete, mother);
	        evas_object_smart_callback_add(bt, "clicked", my_bt_delete_item, todolist);
            /* the button should fill any space it is given horizontally */
            evas_object_size_hint_align_set(bt, -1.0, 0.0);
            /* put the button at the end of the box */
            elm_box_pack_end(bx, bt);
            /* show the button */
            evas_object_show(bt);

	    // elemento successivo
	    todolist = todolist->pun;

	 }
   }

   Evas_Object *bt_quit;
   /* create a button */
   bt_quit = elm_button_add(win);
   window_swallow(ly, "button_quit", bt_quit);
   /* set the button label */
   elm_button_label_set(bt_quit, "Quit");
   /* add a callback to be run when the button is clicked */
   evas_object_smart_callback_add(bt_quit, "clicked", my_win_close, win);
   // FIXME: non refresh correttamente
   evas_object_smart_callback_add(bt_quit, "clicked", my_entry_bt_refresh, mother);

   /* the button should fill any space it is given horizontally */
   evas_object_size_hint_align_set(bt, -1.0, 0.0);
   /* show the button */
   evas_object_show(bt);

   /* set an initial window size */
   evas_object_resize(win, 320, 300);
   /* show the window */
   evas_object_show(win);
   printf ("DEBUG: </my_entry_bt_delete>\n");
}
